import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_getx/GetXControllerExample/GetXControllerExample.dart';
import 'package:flutter_getx/GetXControllerWorkersExample/GetXControllerWorkersExample.dart';
import 'package:flutter_getx/SnakebarExample/snakebarExample.dart';
import 'package:get/get.dart';

import 'DialogExample/dialog_example.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return GetMaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'Flutter Demo',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
        useMaterial3: true,
      ),
      getPages: [
        GetPage(
          name: "/",
          page: () => const MyApp(),
        ),
        GetPage(
          name: "/dialog",
          page: () => const DialogExample(),
        ),
        GetPage(
          name: "/snack-bar",
          page: () => const SnackbarExample(),
        ),
        GetPage(
          name: "/getXController",
          page: () => GetXControllerExample(),
        ),
        GetPage(
          name: "/getXControllerWorkers",
          page: () => GetXControllerWorkersExample(),
        ),
      ],
      home: const DialogExample(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;

  void _incrementCounter() {
    setState(() {
      _counter++;
    });
  }

  void _alertDialog(BuildContext context) async {
    var res = await showDialog(
      context: context,
      builder: (context) {
        return AlertDialog(
          title: const Text("Dialog"),
          content: const Text("This is a dialog"),
          actions: [
            TextButton(
              onPressed: () {
                Navigator.of(context).pop();
              },
              child: const Text("Cancel"),
            ),
            TextButton(
              onPressed: () {
                Navigator.of(context).pop();
              },
              child: const Text("OK"),
            ),
          ],
        );
      },
    );
    if (kDebugMode) {
      print(res);
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            ElevatedButton(
              onPressed: () {
                _alertDialog(context);
              },
              child: const Text("Flutter默认的Dialog"),
            ),
            ElevatedButton(
              onPressed: () {
                Get.defaultDialog(
                  title: "提示信息！",
                  middleText: "您确定要删除吗？",
                  confirm: TextButton(
                    onPressed: () {
                      Get.back();
                    },
                    child: const Text("确定"),
                  ),
                  cancel: TextButton(
                    onPressed: () {
                      Get.back();
                    },
                    child: const Text("取消"),
                  ),
                );
              },
              child: const Text("Getx默认的Dialog"),
            ),
            ElevatedButton(
              onPressed: () {
                Get.snackbar(
                  "提示",
                  "您还没有登录",
                  snackPosition: SnackPosition.TOP,
                );
              },
              child: const Text("Getx snackbar"),
            ),
            ElevatedButton(
              onPressed: () {
                Get.bottomSheet(
                  Container(
                    height: 150,
                    // width: double.infinity,
                    color: Get.isDarkMode ? Colors.black26 : Colors.white,
                    child: Column(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        ListTile(
                          leading: Icon(
                            Icons.wb_sunny_outlined,
                            color:
                                Get.isDarkMode ? Colors.white : Colors.black87,
                          ),
                          title: Text(
                            "浅色模式",
                            style: TextStyle(
                                color: Get.isDarkMode
                                    ? Colors.white
                                    : Colors.black87),
                          ),
                          onTap: () {
                            Get.changeTheme(ThemeData.light());
                            Get.back();
                          },
                        ),
                        ListTile(
                          leading: Icon(
                            Icons.wb_sunny,
                            color:
                                Get.isDarkMode ? Colors.white : Colors.black87,
                          ),
                          title: Text(
                            "深色模式",
                            style: TextStyle(
                                color: Get.isDarkMode
                                    ? Colors.white
                                    : Colors.black87),
                          ),
                          onTap: () {
                            Get.changeTheme(ThemeData.dark());
                            Get.back();
                          },
                        ),
                      ],
                    ),
                  ),
                );
              },
              child: const Text("Getx bottomSheet切换主题"),
            ),
            const SizedBox(
              height: 10,
            ),
            const Text(
              'You have pushed the button this many times:',
            ),
            Text(
              '$_counter',
              style: Theme.of(context).textTheme.headlineMedium,
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: const Icon(Icons.add),
      ), // This trailing comma makes auto-formatting nicer for build methods.
    );
  }
}
